<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>飞得更高</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link href="css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body background="img/02.jpg">

		<div>
			<ul class="nav nav-pills">
				<li role="presentation"><a href="#" onclick="Allplane()">所有航班</a></li>
				<li role="presentation"><a href="#" onclick="Newplane()">新增航班</a></li>
				<li role="presentation"><a href="#">Messages</a></li>
			</ul>
		</div>


		<div id="planeInfo">
			<table class="table table-bordered" id="planeRoute">
				<thead>
					<tr>
						<th>航班号</th>
						<th>航班名称</th>
						<th>起始地点</th>
						<th>目的地</th>
						<th>价格</th>
						<th>剩余票数</th>
						<th>起飞时间</th>
						<th> </th>
						<th> </th>
					</tr>
				</thead>
			</table>
			
			
			
			
			
			
			<nav aria-label="Page navigation">
				<ul class="pagination" id="fenye">
					<li>
						<a href="#" aria-label="Previous">
							<span aria-hidden="true">&laquo;</span>
						</a>
					</li>
					<li><a href="#">1</a></li>
					<li><a href="#">2</a></li>
					<li><a href="#">3</a></li>
					<li><a href="#">4</a></li>
					<li><a href="#">5</a></li>
					<li>
						<a href="#" aria-label="Next">
							<span aria-hidden="true">&raquo;</span>
						</a>
					</li>
				</ul>
			</nav>
			
			
		</div>
		<div id="insert" style="display: none;">
				航班名称:<input type="text" class="form-control" placeholder="planeName" aria-describedby="sizing-addon2" id="planeName">
				起飞时间名称:<input type="datetime" class="form-control" placeholder="YYYY-MM-DD HH:MM:SS" aria-describedby="sizing-addon2" id="time">
				起始地点:<input type="text" class="form-control" placeholder="firsr" aria-describedby="sizing-addon2" id = "first">
				目的地:<input type="text" class="form-control" placeholder="destination" aria-describedby="sizing-addon2" id="destination">
				票价:<input type="text" class="form-control" placeholder="money" aria-describedby="sizing-addon2" id="money">
				票数:<input type="text" class="form-control" placeholder="number" aria-describedby="sizing-addon2" id="number">
				<div class="col-md-offset-1">
					<button type="button" class="layui-btn layui-btn-radius layui-btn-normal btn-block btn btn-primary" onclick="insert()">新增航班</button>
				</div>
				
			</div>


	</body>
	<script src="js/jquery.min.js"></script>
	<script src="js/jquery-3.3.1.min.js"></script>
	<script src="js/bootstrap.min.js"></script>

	<script type="text/javascript">
		function page() {
			var i = 1;

			var assort = $("#fenye li");
			assort.click(function() {
						var classfiyText = $(this).find("a").eq(0).text();
						alert(classfiyText);
			});
		}
		
		function Newplane(){
				$("#planeInfo").hide();
				$("#insert").show();
		}
			
		function Allplane(){
				$("#insert").hide();
				$("#planeInfo").show();
		}
	</script>

<script type="application/javascript">
	function insert(){
		var planeName = $("#planeName").val();
		var time = $("#time").val();
		var first = $("#first").val();
		var destination = $("#destination").val();
		var money = $("#money").val();
		var number = $("#number").val();
		
		if(planeName == ""){
			alert("飞机名不能为空");
			return;
		}
		if(time == ""){
			alert("起飞时间不能为空");
			return;
		}
		if(first == ""){
			alert("起始地点不能为空");
			return;
		}
		if(destination == ""){
			alert("目的地不能为空");
			return;
		}
		
		if(number>40){
			alert("最多只能有40张票");
			return;
		}
		if(number == ""){
			number = 40;
		}
		if(money == ""){
			alert("票价不能为空");
			return ;
		}
		
		var allData = {
					"planeName":planeName,
					"time":time,
					"first":first,
					"destination":destination,
					"money":money,
					"number":number,	
			}
			
			$.ajax({
				async: false,
				type:"POST",
				url: "http://localhost:8080/admin/insertPlane",
				dataType:"json",
				contentType:"application/json;charset=utf-8",
				data:JSON.stringify(allData),
				success:function(data){
					alert(data.message);
					if(data.status == 1){
						//alert("新加了航班号为"+data.id+"的飞机！");
						window.location.href = "http://localhost:8080/admin.html";
					}else{
						window.location.href = "http://localhost:8080/login.html";
					}
					
					return;
				},
				error: function(data) {
					alert("请登录！");
					return;
				},
			})
	}
	
</script>

<script type="application/javascript">
	function deletePlane(n){
	
		$.ajax({
			async: false,
			type:"GET",
			url: "http://localhost:8080/admin/deletePlane?planeId="+n,
			dataType:"json",
			contentType:"application/json;charset=utf-8",
			success:function(data){
				alert(data.message);
				if(data.status == 1){
					window.location.href = "http://localhost:8080/admin.html";
				}else{
					window.location.href = "http://localhost:8080/login.html";
				}
				
				return;
			},
			error: function(data) {
				alert("请登录！");
				return;
			},
		})
	}
	
</script>


	<script type="application/javascript">
		$(function() {

		$.ajax({
			async: false,
			type: "POST",
			url: "http://localhost:8080/plane/index",
			dataType: "json",
			contentType: "application/json;charset=utf-8",
			//data:JSON.stringify(allData),
			//实现json数组输出成表格
			success: function(data) {
				if (data.status == 1) {
					var str;
					$.each(data.result, function(i, n) {
						str += "<tr>";
						str += "<td id = " + n.id + ">" + n.id + "</td>";
						str += "<td>" + n.planeName + "</td>";
						str += "<td>" + n.first + "</td>";
						str += "<td>" + n.destination + "</td>";
						str += "<td>" + n.money + "</td>";
						str += "<td>" + n.number + "</td>";
						str += "<td>" + n.time + "</td>";
						str += "<td><button type='button' class='btn-block btn btn-primary ' onclick='deletePlane(" + n.id +
							")'>删除</button></td>";
						str += "</tr>";
					})
					$("#planeRoute").append(str);
				} else {
					alert(data.message);
				}
				return;
			},
			error: function(data) {
				alert("请先登录");
				window.location.href = "http://localhost:8080/login.html"
				return;
			},

		})

	})
	</script>



</html>
